//
//  Team9SaveViewController.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/26.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9SaveViewController.h"
#import "Team9SaveCell.h"
#import "DataBaseTool.h"
#import <UIScrollView+EmptyDataSet.h>
#import "Team9BaseView.h"

@interface Team9SaveViewController ()<UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@property(nonatomic, strong)NSMutableArray<Team9SaveModel *> *array;
@property(nonatomic, strong)UITableView *tableView;


@end

@implementation Team9SaveViewController
- (void)dealloc{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    self.tableView.emptyDataSetDelegate = nil;
    self.tableView.emptyDataSetSource = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.tableFooterView = [[Team9BaseView alloc] initWithType:ViewTYpeLight];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSArray *array = [[DataBaseTool shareDataBaseTool] selectAllDesigner];
    if (array.count != 0) {
        self.array = [[array reverseObjectEnumerator] allObjects].mutableCopy;
    }
    
}
- (void)setArray:(NSMutableArray<Team9SaveModel *> *)array{
    if (_array != array) {
        _array = array;
        [self.tableView reloadData];
    }
}
#pragma mark 占位图
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    
    NSString *text = nil;
    UIFont *font = nil;
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    font = [UIFont boldSystemFontOfSize:26.0 * WIDTH / 375];
    text = @"还没有收藏";
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (paragraph) [attributes setObject:paragraph forKey:NSParagraphStyleAttributeName];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
    
    return attributedString;

}
#pragma mark tableView methods for protols
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40 * WIDTH /375 + 20;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"取消收藏";
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
// 删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        DataBaseTool *tool = [DataBaseTool shareDataBaseTool];
        Team9SaveModel *model = _array[indexPath.row];
        [tool deleteDesigner:model];
        [self.array removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        if (_array.count == 0) {
            [_tableView reloadData];
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Team9SaveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"saveCell"];
    if (!cell) {
        cell = [[Team9SaveCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"saveCell"];
    }
    cell.vc = self;
    cell.model = self.array[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Team9SaveModel *model = self.array[indexPath.row];
    [Team9Tool viewContrller:self pushBytype:model.author_type andId:model.author_id];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
